package com.sttg.csp.common.util.paged;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 
 */
public class PagedList implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5529301152408992565L;

	/**
	 * default records's number per a page
	 */
	public static final int DEFAULT_PAGE_SIZE = 13;

	// default current page number
	public static final int DEFAULT_PAGE_NUM = 3;
	
	// record's number per a page
	private int pageSize;

	// current page number
	private int pageNum;

	// 实际的数据列表
	private List source;

	// total number of all record.
	private int totalNum = -1;

	// start index.
	private int start = 1;

	public PagedList() {
		this(DEFAULT_PAGE_SIZE, DEFAULT_PAGE_NUM);
	}

	public PagedList(int pageSize, int pageNum) {
		this.pageSize = pageSize;
		this.pageNum = pageNum;
		this.source = Collections.EMPTY_LIST;
	}

	// calculate total pages
	public int getTotalPage() {
		if (this.getTotalNum() <= 0)
			return 0;

		float nrOfPages = (float) this.getTotalNum() / this.pageSize;
		return (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1: nrOfPages);
	}

	// 返回当前页面的数据记录，不指定页码
	public List getList() {
		if (this.getTotalNum() <= 0) {
			return Collections.EMPTY_LIST;
		}

		return this.source;
	}

	public void setSource(List source) {
		this.source = source;
		if (source == null || source.isEmpty()) {
			this.setPageNum(0);
		}
	}

	public List getSource() {
		return source;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageNum() {
		return pageNum;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}
}
